using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

using Microsoft.Practices.EnterpriseLibrary.Logging;

using NUnit.Framework;

namespace Sandia.MVCFramework.Test
{
    /// <summary>
    /// Configuration for exception handling is defined in App.config file,
    /// which should be edited using the Microsoft Enterprise Library
    /// Configuration tool
    /// </summary>
    [TestFixture]
    public class LoggingTests : FailOnErrorLoggingTestFixture
    {
        private const string LOG_FILE = "loggingtest.log";

        [Test]
        public void LoggingTest()
        {
            // Erase existing log (if it exists)
            if (File.Exists(LOG_FILE))
            {
                File.Delete(LOG_FILE);
            }

            // Generate a log entry
            LogEntry logEntry = new LogEntry();
            logEntry.Message = "Test";
            logEntry.EventId = 100;
            logEntry.Priority = 2;
            logEntry.Categories.Add("General");            
            Logger.Write(logEntry);                        

            // Make sure right stuff was written out to log flat file
            // NOTE: Can't directly read LOG_FILE, as it is still in use
            // by the logger, so need to make a copy of it to read
            File.Copy(LOG_FILE, LOG_FILE + ".tmp", true);
            StreamReader reader = new StreamReader(LOG_FILE + ".tmp");
            string line = reader.ReadLine();
            Assert.AreEqual(line, "----------------header------------------------");
            line = reader.ReadLine();
            Assert.IsTrue(line.StartsWith("Timestamp: "));
            line = reader.ReadLine();
            Assert.AreEqual(line, "Message: Test");
            line = reader.ReadLine();
            Assert.AreEqual(line, "Category: General");
            line = reader.ReadLine();
            Assert.AreEqual(line, "Priority: 2");
            line = reader.ReadLine();
            Assert.AreEqual(line, "EventId: 100");
            line = reader.ReadLine();
            Assert.AreEqual(line, "Severity: Information");
            line = reader.ReadLine();
            Assert.AreEqual(line, "Title:");
            line = reader.ReadLine();
            Assert.IsTrue(line.StartsWith("Machine: "));
            line = reader.ReadLine();
            Assert.IsTrue(line.StartsWith("Application Domain: "));
            line = reader.ReadLine();
            Assert.IsTrue(line.StartsWith("Process Id: "));
            line = reader.ReadLine();
            Assert.IsTrue(line.StartsWith("Process Name: "));
            line = reader.ReadLine();
            Assert.IsTrue(line.StartsWith("Win32 Thread Id: "));
            line = reader.ReadLine();
            Assert.IsTrue(line.StartsWith("Thread Name: "));
            line = reader.ReadLine();
            Assert.AreEqual(line, "Extended Properties: ");
            line = reader.ReadLine();
            Assert.AreEqual(line, "----------------footer------------------------");

            reader.Close();

            if (File.Exists(LOG_FILE + ".tmp"))
            {
                File.Delete(LOG_FILE + ".tmp");
            }
        }
    }
}
